package nccloud.web.platform.print;

import com.alibaba.fastjson.JSONObject;
import nc.vo.pub.print.PrintTempletmanageHeaderVO;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.ClientInfo;
import nccloud.framework.web.container.HttpOperator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.RequestSysJsonVO;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.pubitf.platform.print.IPrintInfo;
import nccloud.pubitf.platform.print.IPrintService2;
import nccloud.pubitf.platform.print.vo.BatchPrintInfo;
import nccloud.pubitf.platform.print.vo.PrintInfo;

public class QueryTemplatesAction
  implements ICommonAction
{
  public Object doAction(IRequest request)
  {
    String read = request.read();
    IJson json = JsonFactory.create();
    BatchPrintInfo info = (BatchPrintInfo)json.fromJson(read, BatchPrintInfo.class);

    RequestSysJsonVO sysParm = HttpOperator.getInstance().getRequest().readSysParam();
    String custom = sysParm.getCustom();
    String pk_group = null;
    if (!StringUtils.isEmpty(custom)) {
      JSONObject obj = JSONObject.parseObject(custom);
      if (!StringUtils.isEmpty(obj.getString("pk_group"))) {
        pk_group = obj.getString("pk_group");
        SessionContext.getInstance().getClientInfo().setPk_group(pk_group);
      }
    }

    IPrintInfo para = convertPrintInfo(info);
    IPrintService2 service = (IPrintService2)ServiceLocator.find(IPrintService2.class);
    PrintTempletmanageHeaderVO[] vos = service.getAllPrintTemplateVOs(para);

    return vos;
  }

  private IPrintInfo convertPrintInfo(BatchPrintInfo info) {
    PrintInfo newinfo = new PrintInfo();
    String appcode = info.getAppcode();
    if (appcode == null)
    {
      appcode = info.getFuncode();
    }
    if (appcode == null) {
      appcode = SessionContext.getInstance().getAppcode();
    }
    newinfo.setAppcode(appcode);

    newinfo.setNodeKey(info.getNodekey());
    return newinfo;
  }
}